package com.ymx.darling.mask;

/**
 * 基本的数据脱敏处理
 * @author 爱Java的小于
 */
public class Mask {
    public static final String MOBILE = "mobile";
    public static final String FIXED_PHONE = "fixed_phone";
    public static final String ID_CARD_NUMBER = "id_card_number";
    public static final String CHINESE_NAME = "chinese_name";
    public static final String ADDRESS = "address";
    public static final String EMAIL = "email";
    public static final String PASSWORD = "password";
    public static final String CAR_LICENSE = "car_license";
    public static final String BANK_CARD_NUMBER = "bank_card_number";
    static MaskProcessor MOBILE_PROCESSOR = (data) ->
        data != null && data.startsWith("1") && data.length() == 11 ? mask(data, 3, 4, 4) : data;

    static MaskProcessor FIXED_PHONE_PROCESSOR = (data) ->
        data != null && data.length() > 5 ? mask(data, 3, 2, data.length() - 5) : data;

    static MaskProcessor ID_CARD_NUMBER_PROCESSOR = (data) ->
            data != null && data.length() >= 15 ? mask(data, 3, 4, data.length() - 7) : data;

    static MaskProcessor CHINESE_NAME_PROCESSOR = (data) -> {
        if (data != null) {
            if (data.length() == 2) {
                return data.charAt(0) + "*";
            }

            if (data.length() == 3) {
                return data.charAt(0) + "*" + data.charAt(2);
            }

            if (data.length() == 4) {
                return "**" + data.substring(2, 4);
            }

            if (data.length() > 4) {
                return mask(data, 2, 1, data.length() - 3);
            }
        }

        return data;
    };
    static MaskProcessor ADDRESS_PROCESSOR = (data) -> {
        if (data != null) {
            if (data.length() > 6) {
                return mask(data, 6, 0, 3);
            }

            if (data.length() > 3) {
                return mask(data, 3, 0, 3);
            }
        }

        return data;
    };
    static MaskProcessor EMAIL_PROCESSOR = (data) -> {
        if (data != null && data.contains("@")) {
            int indexOf = data.lastIndexOf("@");
            String email = data.substring(0, indexOf);
            if (email.length() == 1) {
                return "*" + data.substring(indexOf);
            } else if (email.length() == 2) {
                return "**" + data.substring(indexOf);
            } else {
                return email.length() < 5 ? mask(email, 2, 0, email.length() - 2) + data.substring(indexOf) : mask(email, 3, 0, email.length() - 3) + data.substring(indexOf);
            }
        } else {
            return data;
        }
    };
    static MaskProcessor PASSWORD_PROCESSOR = (data) ->
        data != null ? mask(data, 0, 0, data.length()) : null;

    static MaskProcessor CAR_LICENSE_PROCESSOR = (data) ->
        data != null ? mask(data, 3, 1, data.length() - 4) : null;

    static MaskProcessor BANK_CARD_PROCESSOR = (data) ->
        data != null && data.length() >= 8 ? mask(data, 4, 4, 4) : data;

    private Mask() {
    }

    private static String createMask(int count) {
        StringBuilder mask = new StringBuilder();

        for(int i = 0; i < count; ++i) {
            mask.append("*");
        }

        return mask.toString();
    }

    private static String mask(String needToMaskString, int keepFirstCount, int keepLastCount, int maskCount) {
        return needToMaskString.substring(0, keepFirstCount) + createMask(maskCount) + needToMaskString.substring(needToMaskString.length() - keepLastCount);
    }
}
